# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=cglm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.9.1
pkgrel=2
pkgdesc="OpenGL Mathematics (glm) for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/recp/cglm"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/recp/cglm/archive/v${pkgver}.tar.gz"
        0001-cglm-include-fix-arm64.patch)
sha256sums=('ba16ee484c9d5e808ef01e55008a156831e8ff5297f10bbca307adeb827a0913'
            '8fe3fbff513bec69cb5eed8d3fbccd9a03bb5e2dfde18c327eaa280d029f9ce8')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-cglm-include-fix-arm64.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=OFF \
    -DCGLM_STATIC=ON \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=ON \
    -DCGLM_STATIC=OFF \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .


  cd "${srcdir}/${_realname}-${pkgver}/docs"

  ${MINGW_PREFIX}/bin/sphinx-build source ${_realname}
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"

  ctest . || true

  cd "${srcdir}/build-${MSYSTEM}-shared"

  ctest . || true
}

package_cglm() {
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-shared"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  # split docs
  mkdir -p "dest${MINGW_PREFIX}/share/doc/"
  cp -r "${srcdir}/${_realname}-${pkgver}/docs/${_realname}" "dest${MINGW_PREFIX}/share/doc/${_realname}"
}

package_cglm-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
